#!/bin/bash
#
# DMI probe: reads system DMI information
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 12 November 2013
#
#   Copyright 2012-2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


# DMI strings to request
declare -a P_PROBE_DMI_DMISTRINGS=(
   baseboard-asset-tag
   baseboard-manufacturer
   baseboard-product-name
   baseboard-serial-number
   baseboard-version
   bios-release-date
   bios-vendor
   bios-version
   chassis-asset-tag
   chassis-manufacturer
   chassis-serial-number
   chassis-type
   chassis-version
   processor-family
   processor-frequency
   processor-manufacturer
   processor-version
   system-manufacturer
   system-product-name
   system-serial-number
   system-uuid
   system-version
)


## p_probe_dmi_help
##
## Echoes help information for the DMI probe. Returns 0.
##
function p_probe_dmi_help() {
	cat << EOF
The DMI Probe queries the system DMI/SMBIOS table to gather system information
such as the serial number (service tag), bios version, etc. Context values
containing this information are saved to context items named dmi_X, where X
represents the specific DMI string to be queried using dmidecode(8). Run
"dmidecode -s" to see the available strings.

By default, all valid known DMI strings are queried. This behavior can be
overridden in the payload file by calling the set_dmi_probe_strings function,
described below.

Context information is written into the dmi/ tree.

Payload Configuration Directives
--------------------------------

set_dmi_probe_strings <string> [string [string ...]]
  Overrides the default set of DMI strings to request with a customized set
  of strings specified on the command line.
EOF
	return 0
}


## p_probe_dmi_summary
##
## Echoes summary information for the DMI probe. Returns 0.
##
function p_probe_dmi_summary() {
	echo "Collects information from the system DMI/SMBIOS table"
	return 0
}


## p_probe_dmi
##
## Probes the system DMI (SMBIOS) table for the requested DMI strings and
## sets context accordingly. Returns 0 on success, non-zero on failure.
## Continues execution if an error occurs, with the return status set to
## the exit code of the first failure.
##
function p_probe_dmi() {
	local status=0
	local chk
	local _s
	
	for s in "${P_PROBE_DMI_DMISTRINGS[@]}"; do
		_s=$(echo "${s}" | sed s/-/_/g)
		set_context "dmi/${_s}" "$(dmidecode -s ${s})"
		chk=$?
		if [[ ${chk} -ne 0 ]]; then
			[ ${status} -eq 0 ] && status=${chk}
		fi
	done
	
	return ${status}
}


## set_dmi_probe_strings <string> [strings ...]
##
## Sets the DMI probe strings to the list of strings specified. Returns 0.
##
function set_dmi_probe_strings() {
	P_PROBE_DMI_DMISTRINGS="$@"
	return 0
}
